package com.vnaloha.dbunit;

import javax.sql.DataSource;

import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.DatabaseDataSet;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
import org.dbunit.operation.DatabaseOperation;

public class ConfigurationData {
	public void setUpDatabase(DataSource ds) throws Exception {
		// initialize your database connection here
		IDatabaseConnection connection = new DatabaseConnection(ds.getConnection());
		connection.getConfig().setProperty("http://www.dbunit.org/features/batchedStatements", true);
		FlatXmlDataSetBuilder builder = new FlatXmlDataSetBuilder();
		builder.setMetaDataSet(new DatabaseDataSet(connection, false));
//		builder.setCaseSensitiveTableNames(true);
//		builder.setColumnSensing(true);
		// initialize your dataset here
		IDataSet dataSet = builder.build(this.getClass().getClassLoader().getResourceAsStream("dbunit-dataset.xml"));
		try {
			DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
		} finally {
			connection.close();
		}
	}
}
